package com.icss.ah.visitLog.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.icss.ah.base.action.BaseAction;
import com.icss.ah.base.bean.EasyuiDataGridJson;
import com.icss.ah.base.util.DateUtils;
import com.icss.ah.base.util.ResourceLoader;
import com.icss.ah.login.bean.UserInfoBean;
import com.icss.ah.visitLog.service.VisitLogService;

/**
 * 访问记录
 *
 */
@Controller
@Scope("prototype")
public class VisitLogAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	
	@Autowired
	private VisitLogService visitLogService;
	
	private String startDate;//开始时间
	private String endDate;//结束时间
	private String visitType;//访问类型
	private String shopName;//店铺名称
	private String productName;//商品名称
	
	private String dateType;//1:本日，2:最近七天，3：最近一月，4：最近一年
	
	/**
	 * 去访问记录页面
	 * @return
	 */
	public String toVisitLogPage(){
		request = this.getRequest();
		String sDate = "";
		String eDate = DateUtils.getCurrentDate("yyyy-MM-dd");
		if("1".equals(dateType)){//本日
			sDate = eDate;
		}else if("2".equals(dateType)){//最近七天
			sDate = DateUtils.getDayByNum("yyyy-MM-dd",-7);
		}else if("3".equals(dateType)){//最近一月
			sDate = DateUtils.getLastMonthModel(eDate,"yyyy-MM-dd","yyyy-MM-dd",-1);
		}else if("4".equals(dateType)){//最近一年
			sDate = DateUtils.getTimeBeforeYear().substring(0, 10);
		}
		request.setAttribute("startDate", sDate);
		request.setAttribute("endDate", eDate);
		return "toVisitLogPage";
	}
	
	/**
	 * 获取访问记录列表
	 */
	public void getVisitLogList(){

		String userCode=getUserCodeFromSession();								//当前登录人员账号
		UserInfoBean userInfo=getUserInfo(userCode);							//登录人员信息
		String shopId=userInfo.getShopId();										//绑定店铺id
		String corpId=userInfo.getCorpId();   //公众号id 
		
		try {
			Map<String, String> map = new HashMap<String, String>();
			map.put("startIndex", getStartIndexMysql());
			map.put("endIndex", getEndIndexMysql());
			map.put("startDate", startDate);
			map.put("endDate", endDate);
			map.put("corpId", corpId);
			map.put("shopId", shopId);
			map.put("visitType", visitType);
			map.put("shopName", shopName);
			map.put("productName", productName);
			List<Map<String, Object>> list=visitLogService.getVisitLogList(map);
			Long total=visitLogService.getVisitLogTotal(map);
			dataGridJson=new EasyuiDataGridJson();
			dataGridJson.setRows(list);
			dataGridJson.setTotal(total);
			infolog.info("visitLogService|getVisitLogList");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("VisitLogAction|异常：",e);
		}finally {
			str2Resp(JSONObject.fromObject(dataGridJson).toString());
		}
		
	
	}

	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public String getVisitType() {
		return visitType;
	}

	public void setVisitType(String visitType) {
		this.visitType = visitType;
	}

	public String getShopName() {
		return shopName;
	}

	public void setShopName(String shopName) {
		this.shopName = shopName;
	}

	public String getProductName() {
		return productName;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}

	public String getDateType() {
		return dateType;
	}

	public void setDateType(String dateType) {
		this.dateType = dateType;
	}

}
